//全局挂载组件，数据，函数，以及路由守卫等
import Vue from 'vue'
import router from './router';
import {
    maskString
} from "@/utils/tool";
import {
    getUserInfo,
    getToken,
    getUserId
} from "@/utils/auth";
import CustomForm from '@/components/customForm'; //顶部搜索筛选组件
import CustomDescriptions from '@/components/customDescriptions'; //表格顶部描述组件
import StatusTag from '@/components/StatusTag/StatusTag.vue'; //状态标签组件
import Pagination from '@/components/Pagination'; //表格分页组件
import elImageCustom from '@/components/elImageCustom/main';

//挂载超级管理员用户id
Vue.prototype.$superId = '1000000';

//挂载分页数据列表
Vue.prototype.$pageSizeList = [10, 20, 30, 40, 50, 80, 100, 200, 500];

//挂载信息脱敏方法
Vue.prototype.$maskString = maskString;

//挂载全局loading方法
Vue.prototype.$setLoading = (text = 'Loading') => {
    let loading = Vue.prototype.$loading({
        lock: true,
        text: text,
        spinner: 'el-icon-loading',
        background: 'rgba(0, 0, 0, 0.7)'
    });
    return loading;
}

//挂载全局登录用户信息
Vue.prototype.$globalInfo = () => {
    let userInfo = getUserInfo();
    return {
        userName: userInfo.name,
        userId: userInfo.user_id,
        token: userInfo.token
    }
}

// 全局组件挂载
Vue.component('CustomForm', CustomForm);
Vue.component('CustomDescriptions', CustomDescriptions);
Vue.component('StatusTag', StatusTag);
Vue.component('Pagination', Pagination);
Vue.component('elImageCustom', elImageCustom);

//全局路由守卫
router.beforeEach((to, from, next) => {
    if (to.name == 'login') {
        //去登录页 判断是否已登录
        if (getToken()) {
            //已登录 去首页
            next({
                path: '/index'
            })
        } else {
            next()
        }
    } else if (to.name == 'userManage') {
        let userId = getUserId();
        if (userId == Vue.prototype.$superId) {
            next();
        } else {
            next({
                path: '/index'
            })
        }
    }
    next();
    // ...
    // 返回 false 以取消导航
    // return false
})